package com.function.service.impl;

import com.function.mapper.EmailMapper;
import com.function.domin.pojo.Email;
import com.function.service.SendEmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Service
public class SendEmailServiceImpl implements SendEmailService {

    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private EmailMapper emailMapper;

    /**
     * 发送邮件
     * @param emailMessage
     */
    @Transactional
    public void sendEmail(Email emailMessage) {
        //创建一个SimpleMailMessage类型的message变量
        SimpleMailMessage message = new SimpleMailMessage();
        BeanUtils.copyProperties(emailMessage, message);
        message.setTo(emailMessage.getTo());
        //调用mailSender中的方法send方法发送邮件
        mailSender.send(message);
        //将发送的email记录在数据库
        emailMessage.setPostTime(LocalDateTime.now());

    }
}
